package com.llf.Blog.Controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.llf.Blog.Mapper.BlogMapper;
import com.llf.Blog.Pojo.Blog;
import com.llf.Blog.Pojo.BlogTags;
import com.llf.Blog.Pojo.Tag;
import com.llf.Blog.Pojo.Type;
import com.llf.Blog.ServiceImpl.BlogServiceImpl;
import com.llf.Blog.ServiceImpl.BlogTagsServiceImpl;
import com.llf.Blog.ServiceImpl.TagServiceImpl;
import com.llf.Blog.ServiceImpl.TypeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author machenike
 */
@Controller
@Api(value = "提供获取首页信息接口",tags = "首页信息管理")
public class IndexController {

    @Autowired
    private BlogServiceImpl blogService;
    @Autowired
    private BlogTagsServiceImpl blogTagsService;
    @Autowired
    private TypeServiceImpl typeService;
    /**
     * 跳转Index页面，并显示对应信息
     */
    @ApiOperation("首页跳转接口")
    @RequestMapping({"/toIndex","/"})
    public String toIndex(Model model,Integer pageNum, Integer pageSize){
        if (pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,4);
        List<Blog> blog = blogService.getBlog(null);
        PageInfo<Blog> pageInfo = new PageInfo<>(blog);
        PageHelper.clearPage();
        model.addAttribute("page",pageInfo);
        // 根据时间倒序排序 显示6个标题内容
        List<Blog> blogList = blogService.getBlogByTime();
        model.addAttribute("blogList",blogList);
        //限制首页只能显示5个标签
        PageHelper.startPage(1,5);
        ArrayList<BlogTags> tagList = blogTagsService.getCountTag();
        PageInfo<BlogTags> tagsPageInfo = new PageInfo<>(tagList);
        PageHelper.clearPage();
        model.addAttribute("tagList",tagsPageInfo);
        //限制首页只能显示3个分类
        PageHelper.startPage(1,3);
        List<Type> typeList = typeService.getCountType();
        PageInfo<Type> typePageInfo = new PageInfo<>(typeList);
        PageHelper.clearPage();
        model.addAttribute("typeList",typePageInfo);
        return "index";
    }




}
